package com.forum.controller.confession;

import com.common.entity.ApiResult;
import com.common.entity.PageEntity;
import com.common.utils.ImageUpload;
import com.forum.param.*;
import com.forum.pojo.confession.ConfessionWallComment;
import com.forum.pojo.confession.ConfessionWallCommentReply;
import com.forum.service.confession.ConfessionWallCommentReplyService;
import com.forum.service.confession.ConfessionWallCommentService;
import com.forum.service.confession.ConfessionWallService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Tsing
 */
@RestController
@RequestMapping("/confessions")
//@CrossOrigin(origins = "*")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ConfessionController {
    private final ConfessionWallService confessionWallService;
    private final ConfessionWallCommentService commentService;
    private final ConfessionWallCommentReplyService replyService;

    /**
     * 表白墙查询
     * **/
    @GetMapping("/list")
    public ApiResult<Map<String,Object>> getConfessionList(ConfessionQueryParam param){
        return confessionWallService.getConfessionList(param);
    }

    /**
     * 表白墙详情
     * **/
    @GetMapping("/detail/{confessionId}")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<?> getConfessionDetailById(@PathVariable Integer confessionId){
        return confessionWallService.getConfessionDetailById(confessionId);
    }

    /**
     * 获取指定表白墙的评论
     * **/
    @GetMapping("/comments")
    public ApiResult<Map<String,Object>> getCommentByConfession(ConfessionCommentQueryParam param){
        Map<String, Object> comments = commentService.getCommentsByConfession(param);
        return ApiResult.success(comments);
    }

    /**
     * 获取当前用户发布的表白墙
     * **/
    @GetMapping("/current")
    public ApiResult<Map<String,Object>> getConfessionByCurrent(ConfessionQueryParam param){
        return confessionWallService.getConfessionByCurrentUser(param);
    }

    /**
     * 表白墙上传
     * **/
    @PostMapping("/upload")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> confessionUpload(@RequestBody ConfessionWallUploadParam param){
        return confessionWallService.confessionUpload(param);
    }


    /**
     * 发布表白墙评论
     * **/
    @PostMapping("/comment/send")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<String> sendComment(@RequestBody ConfessionSendCommentParam param){
        return commentService.sendComment(param);
    }

    /**
     * 分页获取评论回复
     * **/
    @GetMapping("/replies")
    public ApiResult<Map<String,Object>> getRepliesByComment(ConfessionCommentReplyQueryParam param){
        return replyService.getRepliesByComment(param);
    }

    /**
     * 发布回复
     * **/
    @PostMapping("/reply/send")
    public ApiResult<String> sendReply(@RequestBody  ConfessionWallCommentReply reply){
        return replyService.sendReply(reply);
    }


    /**
     * 多张图片的上传
     * @param files 图片路径地址
     * @return
     */
    @PostMapping("/imgs/upload")
    @SneakyThrows(IOException.class)
    public List<String> uploadImgs(MultipartFile[] files){
        return ImageUpload. UploadImgs(files);
    }

}
